from pyglet.gl import *
from awesum import Drawable, Vector3, Game, Quaternion

class Weapon(Drawable):
	def __init__(self, car):
		super(Weapon, self).__init__(Drawable.Traits.GL | Drawable.Traits.MOVE)
		self.origin = car.origin + car.move.quat.rotateV(Vector3(0., 0., 3.))
		self.move.quat = car.move.quat
		self.move.speed = 1.2
		self.curTrackPart = car.curTrackPart
		self.distTraveled = 0.
		self.moved = Vector3(0., 0., .11)
	
	def load(self, game):
		self.gl.id = game.models['missile']
		super(Weapon, self).load(game)
		self.gl.translate = Vector3(0., 1., 0.)
		self.gl.scaleV(Vector3(2., 2., 2.))
		self.gl.rotation = Vector3(0., 0., 0.)
		self.move.drawRotation = True
	def draw(self):
		self.game.setColor(.8, 0., .3)
		super(Weapon, self).draw()
	def step(self):
		super(Weapon, self).step()
		self.gl.rotation.z += 5.
		self.gl.rotation.z %= 360.
		self.move.updateRot()
		movedDist = self.moved.magnitude()
		self.distTraveled += movedDist
		if self.distTraveled > 200. or movedDist < .1:
			self.delete()
	
	@staticmethod
	def loadModels(game):
		"""Preload weapon models, this should be called from the parent scene's load() method"""
		if "missile" not in game.models:
			id = game.models["missile"]
			game.loadModel("missile.stl", Game.ModelType.STL_BIN, id)
	
	def loadModelOld(game):
		id = game.models['missile']
		left = -.2
		right = .2
		front = -.5
		back = .5
		top = .4
		bottom = 0
		
		glNewList(id, GL_COMPILE)
		glColor3f(0., .6, .2)
		glBegin(GL_LINE_STRIP)	#Top square
		glVertex3f(left, top, back)
		glVertex3f(right, top, back)
		glVertex3f(right, top, front)
		glVertex3f(left, top, front)
		glVertex3f(left, top, back)
		glEnd()
		glBegin(GL_LINE_STRIP)	#Bottom square
		glVertex3f(left, bottom, back)
		glVertex3f(right, bottom, back)
		glVertex3f(right, bottom, front)
		glVertex3f(left, bottom, front)
		glVertex3f(left, bottom, back)
		glEnd()
		glBegin(GL_LINES)
		glVertex3f(left, top, back)
		glVertex3f(left, bottom, back)
		glVertex3f(right, top, back)
		glVertex3f(right, bottom, back)
		glVertex3f(right, top, front)
		glVertex3f(right, bottom, front)
		glVertex3f(left, top, front)
		glVertex3f(left, bottom, front)
		glVertex3f(left, top, back)
		glVertex3f(left, bottom, back)
		glEnd()
		glEndList()
		
		return id